from myro import *

init("COM40")

#helper functions
def turnLeft90():
    turnLeft(0.5, 1.6)

def turnRight90():
    turnRight(0.5, 1.6)
    
def getAllObstacles():
    return [getObstacle(x) for x in xrange(3)]

def isObstacle(threshold):
    readings=[0, 0, 0]
    for _ in xrange(10):
        for i in xrange(3):
            readings[i]+=getObstacle(i)
        wait(0.1)
    return sum(map(sum, readings))/10>threshold, turnLeft90 if readings[0]<readings[2] else turnRight90

def oppositeTurn(direction):
    if direction==turnLeft90:
        return turnRight90
    else:
        return turnLeft90
        

#main program    
def goForward():
    while 1:
        obstacle, direction=isObstacle(1000)
        if obstacle:
            return direction
        forward(0.5, 1)
        
def zigZag(direction):
    while 1:
        forward(0.5, 1)
        if 
    direction()
    forward(0.5, 1)
    oppositeDirection()
        
def avoidObstacle(direction):
    while 1:
        direction()
        forward(0.5,1)
        oppositeDirection(direction)()
        if not isObstacle(300)[0]:
            return     
    
def passObstacle(direction):
    for _ in xrange(3):
        forward(0.5, 1)
        if 
    while 1:
        
    